package com.frxuexi.shop2307.user

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.SpannableString
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.util.Log
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.CodeBean
import com.frxuexi.lib_base.http.Const
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.lib_base.utils.LiveDataBus
import com.frxuexi.shop2307.R
import com.frxuexi.shop2307.databinding.ActivityRegisterBinding
import com.google.gson.Gson
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.Timer
import kotlin.concurrent.timerTask

@Route(path = "/app/RegisterActivity")
@AndroidEntryPoint
class RegisterActivity : BaseActivity<ActivityRegisterBinding,UserViewModel>() {

    var timer:Timer?=null
    var num=10

    override fun initData() {
        vm.codeData.observe(this){
            val codeBean = Gson().fromJson<CodeBean>(it.content, CodeBean::class.java)
            ToastUtils.showLong(codeBean.code)
        }

        vm.registerData.observe(this){
            Log.i("lr",it.toString())
            ToastUtils.showLong("注册成功")
            ARouter.getInstance().build("/app/LoginActivity").withString("user","${it.phone}").navigation()
            finish()
        }

        vm.loginError.observe(this){
            ToastUtils.showLong(it)
        }

    }

    override fun initView() {
        binding.registerBtn.setOnClickListener {
           /* val name="admin"
            val pass="666"

//            LiveDataBus.with("user",String::class.java).postValue("${name},${pass}")
            ARouter.getInstance().build("/app/LoginActivity").withString("user","${name},${pass}").navigation()
            finish()*/
            val phone = binding.registerPhone.text.toString()
            val code = binding.registerCode.text.toString()
            val pass = binding.registerPass.text.toString()
            val pass2 = binding.registerPass2.text.toString()

            if(phone.isNullOrEmpty() || code.isNullOrEmpty() || pass.isNullOrEmpty() || pass2.isNullOrEmpty()){
                ToastUtils.showLong("输入内容不能为空")
                return@setOnClickListener
            }
            /*if(!RegexUtils.isEmail(phone)){
                ToastUtils.showLong("不是合法邮箱")
                return@setOnClickListener
            }*/
            if(!binding.registerRemember.isChecked){
                ToastUtils.showLong("请勾选")
                return@setOnClickListener
            }
            val map = mapOf<String, Any>("phone" to phone, "captcha" to code)
            vm.register(map)

        }

        binding.registerSendCode.setOnClickListener {
            val phone = binding.registerPhone.text.toString()
            if(phone.isNullOrEmpty()){
                ToastUtils.showLong("手机号不能为空")
                return@setOnClickListener
            }
            if(!RegexUtils.isMobileSimple(phone)){
                ToastUtils.showLong("不是合法手机号")
                return@setOnClickListener
            }
            binding.registerSendCode.isEnabled=false
            lifecycleScope.launch(Dispatchers.IO) {
                timer= Timer()
                timer!!.schedule(timerTask {
                    lifecycleScope.launch (Dispatchers.Main){
                        binding.registerSendCode.setText("还剩 ${num--}s")
                        if(num==0){
                            timer!!.cancel()
                            binding.registerSendCode.isEnabled=true
                            binding.registerSendCode.setText("重新发送")
                            num=10
                        }
                    }
                },0,1000)
            }
            vm.sendCode(phone)
        }

        val rem = binding.registerRemember.text.toString()
        val span = SpannableString(rem)
        span.setSpan(object :ClickableSpan(){
            override fun onClick(widget: View) {
                ARouter.getInstance().build("/app/WebViewActivity").navigation()
            }
        },3,7,SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE)

        binding.registerRemember.text=span
        binding.registerRemember.movementMethod=LinkMovementMethod.getInstance()



    }

}